"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongodbManager = void 0;
const DbConfig_1 = require("../../config/DbConfig");
const Logger_1 = require("./Logger");
const MongodbClient_1 = require("./MongodbClient");
class MongodbManager {
    constructor() {
        this.dbMap = new Map();
    }
    init() {
        return __awaiter(this, void 0, void 0, function* () {
        });
    }
    createClient(dbName) {
        return __awaiter(this, void 0, void 0, function* () {
            let dbConfig = DbConfig_1.DbConfig.getDbConfig(DbConfig_1.DbConfig.now_db_type);
            let client = yield MongodbClient_1.MongodbClient.create(dbName, dbConfig.host, dbConfig.port, dbConfig.user, dbConfig.pass);
            this.dbMap.set(dbName, client);
            return client;
        });
    }
    getDb(dbName) {
        return __awaiter(this, void 0, void 0, function* () {
            let db;
            if (this.dbMap.has(dbName)) {
                db = this.dbMap.get(dbName);
            }
            else {
                let dbType = DbConfig_1.DbConfig.now_db_type;
                Logger_1.Logger.info("数据库client", dbName, dbType);
                db = yield this.createClient(dbName);
            }
            return db;
        });
    }
}
exports.MongodbManager = MongodbManager;
MongodbManager.instance = new MongodbManager();
